<?php
declare(strict_types=1);

namespace Service\ConsumerService\ConsumerMapping;

use Service\BaseService;

class CommodityChangeMapping extends BaseService
{
    const TO_PUSH_FIELD = [];

    /**
     * 对比是否需要推送的数据
     * @param $communityMappings
     * @return bool
     */
    public function checkCommodityArchivesChange($communityMappings): bool
    {
        //业务上需要判断两个字段国际条码(barCode)和商品类型(goodsType)
        $archivesChange = [];
        $archivesChange[] = ['fieldName' => CommodityMapping::instance()->barcode];
        $archivesChange[] = ['fieldName' => CommodityMapping::instance()->goodstype];
        $result = array_diff_assoc(
            $this->getCompareData($communityMappings['goodsBaseMapping']['base'], $archivesChange),
            $this->getCompareData($communityMappings['oldGoodsBaseMapping']['base'], $archivesChange)
        );
        return !empty($result);
    }

    /**
     * 对比是否需要推送的数据
     * @param $communityMappings
     * @param $communityChange
     * @return bool
     */
    public function checkCommodityUpdateChange($communityMappings, $communityChange): bool
    {
        //业务上需要判断两个字段国际条码(barCode)和商品类型(goodsType)
        $result = array_diff_assoc(
            $this->getCompareData($communityMappings['goodsBaseMapping']['base'], $communityChange),
            $this->getCompareData($communityMappings['oldGoodsBaseMapping']['base'], $communityChange)
        );

        return !empty($result);
    }

    /**
     * 对比是否需要推送的数据
     * @param $communityMappings
     * @param $communityChange
     * @return bool
     */
    public function checkStockInfoUpdateChange($stockMappings, $supplierInfo): bool
    {
        //业务上需要判断两个字段国际条码(barCode)和商品类型(goodsType)
        $result = array_diff_assoc(
            $this->getStockCompareData($stockMappings['goodsBaseMapping']['stock'], $supplierInfo['CanModifyPrice']),
            $this->getStockCompareData($stockMappings['oldGoodsBaseMapping']['stock'], $supplierInfo['CanModifyPrice'])
        );
        return !empty($result);
    }


    /**
     * 获取比对的数据
     * @param $data
     * @param $fields
     * @return array
     */
    public function getCompareData($data, $fields): array
    {
        $compareData = [];
        foreach ($fields as $field) {
            $fieldName = $field['fieldName'];
            if ($fieldName == CommodityMapping::instance()->taxRate) {
                $compareData[$fieldName] = sprintf("%.4f", ($data[$fieldName] ?? $field['defaultValue']));
            }
            if (isset($field['defaultValue'])) {
                $compareData[$fieldName] = $data[$fieldName] ?? $field['defaultValue'];
            } else {
                $compareData[$fieldName] = $data[$fieldName];
            }
        }
        return $compareData;
    }


    /**
     * 获取库存的比对数据
     * @param $data
     * @param $CanModifyPrice
     * @return array
     */
    public function getStockCompareData($data, $CanModifyPrice): array
    {
        $stockCompareData = [];
        $stockInfoMapping = StockInfoMapping::instance();
        foreach ($data as $datum) {
            $stockCompareData[$stockInfoMapping->batchNo] = $datum[$stockInfoMapping->batchNo];
            $stockCompareData[$stockInfoMapping->number] = $datum[$stockInfoMapping->number];
            $stockCompareData[$stockInfoMapping->BatchID] = $datum[$stockInfoMapping->BatchID] ?? '';
            if (!$CanModifyPrice) {
                $stockCompareData[$stockInfoMapping->specList] = is_array($datum[$stockInfoMapping->specList]) ? $datum[$stockInfoMapping->specList] : (json_decode($datum[$stockInfoMapping->specList], true) ?? []);
                foreach ($stockCompareData[$stockInfoMapping->specList] as &$v) {
                    if (isset($v[$stockInfoMapping->SpecFreightPrice])) {
                        unset($v[$stockInfoMapping->SpecFreightPrice]);
                    }
                    unset($v);
                }
                $stockCompareData[$stockInfoMapping->isHasPrice] = $datum[$stockInfoMapping->isHasPrice];
            }
        }
        return $stockCompareData;
    }


}